<template>
  <view class="privacy" v-if="showPrivacy">
    <view class="content">
      <view class="title">隐私保护指引</view>
      <view class="des">
        在使用当前小程序服务之前，请仔细阅读
        <text class="link" @click="openPrivacyContract">{{ privacyContractName }}</text>
        。如果你同意{{ privacyContractName }}，请点击“同意”开始使用。
      </view>
      <view class="btns">
        <view class="item reject" @click="exitMiniProgram">拒绝</view>
        <button id="agree-btn" class="item agree" open-type="agreePrivacyAuthorization" @agreeprivacyauthorization="handleAgreePrivacyAuthorization">
          同意
        </button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';

// 响应式数据
const showPrivacy = ref(false);
const privacyContractName = ref('');

// 获取隐私设置
wx.getPrivacySetting({
  success: (res) => {
    if (res.needAuthorization) {
      privacyContractName.value = res.privacyContractName;
      showPrivacy.value = true;
    }
  },
});

// 同意隐私协议
const handleAgreePrivacyAuthorization = () => {
  showPrivacy.value = false;
};

// 拒绝隐私协议
const exitMiniProgram = () => {
  uni.showModal({
    content: '我们将无法获取您的信息, 包括手机号、相册等该小程序十分重要的功能,您确定要拒绝吗?',
    success: (res) => {
      if (res.confirm) {
        showPrivacy.value = false;
      }
    },
  });
};

// 跳转协议页面
const openPrivacyContract = () => {
  wx.openPrivacyContract({
    fail: () => {
      uni.showToast({
        title: '网络错误',
        icon: 'error',
      });
    },
  });
};
</script>

<style lang="scss" scoped>
.privacy {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 9999999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);

  .content {
    box-sizing: border-box;
    width: 85vw;
    padding: 50rpx;
    background: #fff;
    border-radius: 16rpx;

    .title {
      font-size: 34rpx;
      font-weight: bold;
      color: #333;
      text-align: center;
    }

    .des {
      margin-top: 40rpx;
      font-size: 26rpx;
      line-height: 1.6;
      color: #666;
      text-align: justify;

      .link {
        color: #07c160;
        text-decoration: underline;
      }
    }

    .btns {
      display: flex;
      justify-content: space-between;
      margin-top: 60rpx;

      .item {
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: space-between;
        justify-content: center;
        width: 244rpx;
        height: 80rpx;
        border: none;
        border-radius: 16rpx;
      }

      .reject {
        color: #909399;
        background: #f4f4f5;
      }

      .agree {
        color: #fff;
        background: #07c160;
      }
    }
  }
}
</style>